package org.dubnation.car.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dubnation.auth.context.LoginContext;
import org.dubnation.base.enums.ResponseCode;
import org.dubnation.base.exception.BusinessException;
import org.dubnation.car.domain.CarDetail;
import org.dubnation.car.mapper.CarDetailMapper;
import org.dubnation.car.service.ICarDetailService;
import org.dubnation.base.service.impl.BaseServiceImpl;
import org.dubnation.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Iterator;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2023-11-02
 */
@Service
public class CarDetailServiceImpl extends BaseServiceImpl<CarDetail> implements ICarDetailService {

    @Autowired(required = false)
    private CarDetailMapper carDetailMapper;

    /**
     * 上传检测报告
     * @param carDetail
     * @param request
     */
    @Override
    @Transactional
    public void uploadReport(CarDetail carDetail, HttpServletRequest request) {
        Object loginAdmin = LoginContext.getLoginAdmin();
        if (loginAdmin instanceof Employee){
            //判断为员工登录, 记录员工信息
            carDetail.setDetectorId(((Employee) loginAdmin).getId());
            carDetail.setDetectorName(((Employee) loginAdmin).getRealName());
        }else throw new BusinessException(ResponseCode.RESPONSE_CODE_4000022);
        carDetail.setDetectiontime(new Date());
        if (carDetail.getId() != null){
            //该车辆已有检测报告, 进行修改即可
            carDetailMapper.update(carDetail);
        }else {
            //没有检测报告, 新增
            carDetailMapper.insert(carDetail);
        }
    }
}
